<?php

class ReportPaymentClass 
{
	public $_explicitType = "presstrack.ReportPaymentClass";
    
	public $arMonthByPayment;
	public $arCustomer;
		
	static function GetPaymentReport($year)
	{
		set_time_limit(0);	
		$result->status = false;
		$report = new ReportPaymentClass();
		
		// array of customer
		$sql ="SELECT * FROM press_track_new.user WHERE medium > 0";
		$query = mysql_query($sql);
		if (mysql_errno())
		{
			$result->message = mysql_errno() . ": " . mysql_error();
			return $result;
		}		
		$ar_of_customer = array();
		while ($res = mysql_fetch_object($query)) 
		{
			$ar_of_customer[] = $res;
		}
		
		// sum of payments
		for ($i=1; $i < 13; $i++) 
		{
			$month = $i;
			if ($i < 10)
			{
				$month = "0" . $month;
			}
			$date = $month . "/" . $year;
			
			$ar_by_month = array();
			foreach ($ar_of_customer as $customer)
			{
				$sql = "SELECT SUM(sum) AS sum_of_payment  FROM payment "
					 . "WHERE creation LIKE '%$date' AND payment.order "
					 . "IN (SELECT idorder FROM press_track_new.order "
					 . "WHERE press_track_new.order.customer = $customer->iduser)";
				 
				$query = mysql_query($sql);
				$res = mysql_fetch_object($query);
				$row->name = $customer->name;
				$row->first = 0;
				$row->second = 0;
				
				if ($res->sum_of_payment != null)
				{
					$row->first = round($res->sum_of_payment, 2);
					$ar_by_month[] = clone $row;
				}				
			}
			$report->arCustomer[]	= $ar_by_month;
			$ar_by_month			= null;
		}
		
		$i = 1;
		foreach ($report->arCustomer as $month)
		{
			$m = $i;
			if ($i < 10)
			{
				$m = "0" . $m;
			}
			$row2->name		= $m;
			$row2->first	= 0;
			foreach ($month as $sum)
			{
				$row2->first = $row2->first + $sum->first;
			}
			$report->arMonthByPayment[] = clone $row2;
			$i++;
		}
		
		$result->status = true;
		$result->report = $report;
		return $result;
	}	
}
?>